/**
 * Created by Administrator on 2017/9/11.
 */
let msg = require('../util/msg.js');
let mem = require('../util/mem.js');
let userDao = require('../mysql/userDao.js');
let chipDao = require('../mysql/chipDao.js');
let variable = require('../data/variable.js');
let gameData = require('../util/gameData.js');

let method = module.exports;

method.trade = function (userId, cid, uuid, data) {
    if (!gameData.d.chipShop[data.id]) {
        msg.send(cid, uuid, 'error', {'api': 'activityChipSell', 'code': 10078});
        return false;
    }
    if (gameData.d.chipShop[data.id].goods == 'gold' ||
        gameData.d.chipShop[data.id].goods == 'score' ||
        gameData.d.chipShop[data.id].goods == 'gem') {
        method.tradeSell(userId, cid, uuid, gameData.d.chipShop[data.id]);
    } else {
        method.tradeBuy(userId, cid, uuid, gameData.d.chipShop[data.id]);
    }
};
method.tradeBuy = function (userId, cid, uuid, shop) {
    let userModel = new userDao();
    userModel.inId(userId, function (err, res) {
        if (res == null) {
            msg.send(cid, uuid, 'error', {'api': 'activityChipBuy', 'code': 10062});
            return false;
        }
        if (res.res[shop.coin] < shop.coinAmount) {
            msg.send(cid, uuid, 'error', {'code': 10074});
            return false;
        }
        let chipModel = new chipDao();
        chipModel.inChipId(userId, shop.goods, function (err2, res2) {
            let resultData = {};
            userModel.add(shop.coin, -shop.coinAmount);
            resultData[shop.coin] = {'now': userModel.data[shop.coin], 'add': -shop.coinAmount};
            userModel.save(function (err1, res1) {
                msg.send(cid, uuid, 'globalUserChange', resultData);
                method.setActivityChip(userId, shop.coin, userModel.data[shop.coin]);
            });

            let resultData2 = {};
            chipModel.addNum(shop.goodsAmount);
            resultData2[shop.goods] = {'now': chipModel.data.num, 'add': shop.goodsAmount};
            chipModel.save(function (err3, res3) {
                msg.send(cid, uuid, 'globalChipChange', resultData2);
                msg.send(cid, uuid, 'globalAlert', {
                    'text': '兑换' + shop.goodsAmount + gameData.d.chip[shop.goods].name + '成功',
                    'title': null
                });
                msg.send(cid, uuid, 'activityChipTrade');
                method.setActivityChip(userId, shop.goods, chipModel.data.num);
            });
        });
    });
};
method.tradeSell = function (userId, cid, uuid, shop) {
    let chipModel = new chipDao();
    chipModel.inChipId(userId, shop.coin, function (err, res) {
        if (!res) {
            msg.send(cid, uuid, 'error', {'api': 'activityChipSell', 'code': 10075});
            return false;
        }
        if (res.res.num < shop.coinAmount) {
            msg.send(cid, uuid, 'error', {'api': 'activityChipSell', 'code': 10075});
            return false;
        }
        let userModel = new userDao();
        userModel.inId(userId, function (err1, res1) {
            if (res1 == null) {
                msg.send(cid, uuid, 'error', {'api': 'activityChipBuy', 'code': 10062});
                return false;
            }
            if (res1.res.tradeGold + shop.goodsAmount >= gameData.d.config.dailyTradeGold) {
                msg.send(cid, uuid, 'error', {'api': 'activityChipBuy', 'code': 10077});
                return false;
            }
            let resultData = {};
            chipModel.addNum(-shop.coinAmount);
            resultData[shop.coin] = {'now': chipModel.data.num, 'add': -shop.coinAmount};
            chipModel.save(function (err2, res2) {
                msg.send(cid, uuid, 'globalChipChange', resultData);
                method.setActivityChip(userId, shop.coin, chipModel.data.num);
            });

            let resultData2 = {};
            userModel.add(shop.goods, shop.goodsAmount);
            resultData2[shop.goods] = {'now': userModel.data[shop.goods], 'add': shop.goodsAmount};
            userModel.add('tradeGold', shop.goodsAmount);
            resultData2.tradeGold = {'now': userModel.data.tradeGold, 'add': shop.goodsAmount};
            userModel.save(function (err3, res3) {
                msg.send(cid, uuid, 'globalUserChange', resultData2);
                msg.send(cid, uuid, 'globalAlert', {
                    'text': '兑换' + shop.goodsAmount + '金币成功',
                    'title': null
                });
                msg.send(cid, uuid, 'activityChipTrade');
                method.setActivityChip(userId, shop.goods, userModel.data[shop.goods]);
            });
        });
    });
};
method.buy = function (userId, cid, uuid, data) {
    let userModel = new userDao();
    userModel.inId(userId, function (err, res) {
        if (res == null) {
            msg.send(cid, uuid, 'error', {'api': 'activityChipBuy', 'code': 10062});
            return false;
        }
        if (!gameData.d.chip[data.chipId]) {
            msg.send(cid, uuid, 'error', {'code': 10077});
            return false;
        }
        if (res.res.gold < gameData.d.chip[data.chipId].buy * data.num) {
            msg.send(cid, uuid, 'error', {'code': 10074});
            return false;
        }
        let chipModel = new chipDao();
        chipModel.inChipId(userId, data.chipId, function (err2, res2) {
            let resultData = {};
            let add = -(gameData.d.chip[data.chipId].buy * data.num);
            userModel.add('gold', add);
            resultData.gold = {'now': userModel.data.gold, 'add': add};
            userModel.save(function (err1, res1) {
                msg.send(cid, uuid, 'globalUserChange', resultData);
                method.setActivityChip(userId, 'gold', userModel.data.gold);
            });

            let resultData2 = {};
            chipModel.addNum(data.num);
            resultData2[data.chipId] = {'now': chipModel.data.num, 'add': data.num};
            chipModel.save(function (err3, res3) {
                msg.send(cid, uuid, 'globalChipChange', resultData2);
                method.setActivityChip(userId, data.chipId, chipModel.data.num);
            });
        });
    });
};
method.sell = function (userId, cid, uuid, data) {
    let chipModel = new chipDao();
    chipModel.inChipId(userId, data.chipId, function (err, res) {
        if (!res) {
            msg.send(cid, uuid, 'error', {'api': 'activityChipSell', 'code': 10075});
            return false;
        }
        if (res.res.num < data.num) {
            msg.send(cid, uuid, 'error', {'api': 'activityChipSell', 'code': 10075});
            return false;
        }
        let userModel = new userDao();
        userModel.inId(userId, function (err1, res1) {
            if (res1 == null) {
                msg.send(cid, uuid, 'error', {'api': 'activityChipBuy', 'code': 10062});
                return false;
            }
            let resultData = {};
            chipModel.addNum(-data.num);
            resultData[data.chipId] = {'now': chipModel.data.num, 'add': -data.num};
            chipModel.save(function (err2, res2) {
                msg.send(cid, uuid, 'globalChipChange', resultData);
                method.setActivityChip(userId, data.chipId, chipModel.data.num);
            });

            let resultData2 = {};
            let add = gameData.d.chip[data.chipId].sell * data.num;
            userModel.add('gold', add);
            resultData2.gold = {'now': userModel.data.gold, 'add': add};
            userModel.save(function (err3, res3) {
                msg.send(cid, uuid, 'globalUserChange', resultData2);
                method.setActivityChip(userId, 'gold', userModel.data.gold);
            });
        });
    });
};
method.setActivityChip = function (userId, key, value) {
    let fields = {};
    switch (key) {
        case 'gold':
            fields.slotMachinePlayers = 'gold';
            break;
        case '300001':
            fields.slotMachinePlayers = 'chip';
            break;
    }
    for (let name in fields) {
        if (!mem[name]) {
            continue;
        }
        if (!mem[name][userId]) {
            continue;
        }
        mem[name][userId][fields[name]] = value;
    }
    return true;
};